<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-yellow sidebar-mini">

<!--创建权限模态框-->
<div class="modal fade" data-backdrop="static" data-keyboard="false" id="modal-create-menu">
    <div class="modal-dialog" style="width:500px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">权限信息</h4>
            </div>
            <div class="modal-body">
                <form id="menuForm" class="form-horizontal">
                    <input type="hidden" id="id" name="id">
                    <input type="hidden" id="pid" name="pid">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">权限名</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name" placeholder="请输入权限名">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">权限码</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="code" placeholder="请输入权限码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">分组排序</label>
                        <div class="col-sm-6">
                            <input type="number" class="form-control" name="orderNo" placeholder="请输入分组排序">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" shiro:haspermission="system:menu:edit" class="btn btn-info"
                        id="btn-create-menu">提交
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<section class="content-header">
    <h1>权限列表</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>名称</label>
                                    <input type="text" name="name" class="form-control"
                                           placeholder="请输入权限名">
                                </div>
                                <div class="form-group">
                                    <label>权限</label>
                                    <input type="text" name="code" class="form-control"
                                           placeholder="请输入权限码">
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search"
                                        onclick="js.treeTable.search(table)">
                                    <span class="Bold">搜索</span>
                                </button>
                                <a href="javascript:void(0);" shiro:haspermission="system:menu:edit"
                                   class="btn btn-info pull-right" onclick="openCreateMenuModal()">
                                    <span class="Bold">添加</span>
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-tree-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var table;

    /** 打开创建权限模态框 */
    function openCreateMenuModal(pid) {
        if (pid) {
            $("#pid").val(pid);
        } else {
            $("#pid").val('');
        }
        js.modal.open("modal-create-menu");
    }

    /** 打开编辑模态框 */
    function openEditMenuModal(result) {
        $("#menuForm").fillData(result);
        js.modal.open("modal-create-menu");
        setTimeout(function () {
            js.validate.validing("menuForm");
        }, 500);
    }

    /** 删除权限 */
    function delMenu(id) {
        js.modal.confirm("删除权限将同时删除所有子级权限，确认删除吗？", function () {
            js.post({
                url: ctx + "sys/menu/del",
                data: {id: id},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.treeTable.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            })
        })
    }

    $(function () {

        /** 加载列表 */
        table = js.treeTable.init({
            code: "id",
            parentCode: "pid",
            uniqueId: "id",
            url: ctx + "sys/menu/list",
            columns: [
                {
                    title: '权限名称', field: 'name', align: "left", width: '20%',
                    formatter: function (value, row, index) {
                        return '<i class="fa fa-map-o"></i> <span class="nav-label">' + value + '</span>';
                    }
                },
                {title: '权限码', field: 'code'},
                {title: '排序', field: 'orderNo'},
                {
                    title: '操作',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (hasP('system:menu:edit')) {
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="openCreateMenuModal(' + row.id + ')"> 新增</a> ');
                            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="openEditMenuModal(' + js.jsonStringify(row) + ')"> 编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="delMenu(' + row.id + ')"> 删除</a>');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 初始化验证 */
        js.validate.init("menuForm", {
            fields: {
                code: {
                    validators: {
                        notEmpty: {message: '请输入权限码'},
                        remote: {
                            url: ctx + "sys/menu/checkUnique",
                            message: '权限码已存在',
                            delay: 200,
                            type: 'POST',
                            data: {
                                id: function () {
                                    return $("#id").val();
                                }
                            }
                        }
                    }
                },
                name: {validators: {notEmpty: {message: '请输入权限名称'}}}
            }
        });

        /** 创建或编辑权限 */
        $("#btn-create-menu").click(function () {
            var url = ctx + "sys/menu/save";
            if ($("#id").val()) {
                url = ctx + "sys/menu/update";
            }
            js.validSubmit({
                formId: "menuForm",
                url: url,
                data: new FormData($("#menuForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.treeTable.refresh(table);
                        js.modal.hide("modal-create-menu");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        });

        /** 隐藏 modal-create-menu */
        js.modal.hideEvent("modal-create-menu", function () {
            js.validate.reset("menuForm");
            js.reset("menuForm");
        })
    });
</script>
</body>
</html>
